{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": 0,
      "metadata": {
        "colab": {
          "autoexec": {
            "startup": false,
            "wait_interval": 0
          }
        },
        "colab_type": "code",
        "id": "gA3ln8L8TGtx"
      },
      "outputs": [],
      "source": [
        "# Copyright 2017 The Sonnet Authors. All Rights Reserved.\n",
        "#\n",
        "# Licensed under the Apache License, Version 2.0 (the \"License\");\n",
        "# you may not use this file except in compliance with the License.\n",
        "# You may obtain a copy of the License at\n",
        "#\n",
        "#    http://www.apache.org/licenses/LICENSE-2.0\n",
        "#\n",
        "# Unless required by applicable law or agreed to in writing, software\n",
        "# distributed under the License is distributed on an \"AS IS\" BASIS,\n",
        "# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or  implied.\n",
        "# See the License for the specific language governing permissions and\n",
        "# limitations under the License.\n",
        "# ============================================================================"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 0,
      "metadata": {
        "cellView": "both",
        "colab": {
          "autoexec": {
            "startup": false,
            "wait_interval": 0
          }
        },
        "colab_type": "code",
        "id": "IqFpO6fIkXfO"
      },
      "outputs": [],
      "source": [
        "#@title Imports\n",
        "\n",
        "from __future__ import absolute_import\n",
        "from __future__ import division\n",
        "from __future__ import print_function\n",
        "\n",
        "# Dependency imports\n",
        "\n",
        "import numpy as np\n",
        "import pandas as pd\n",
        "\n",
        "import matplotlib.pyplot as plt\n",
        "import matplotlib.lines as mlines\n",
        "\n",
        "import sonnet as snt\n",
        "import seaborn as sns\n",
        "\n",
        "from matplotlib import colors\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 0,
      "metadata": {
        "colab": {
          "autoexec": {
            "startup": false,
            "wait_interval": 0
          }
        },
        "colab_type": "code",
        "id": "u37AHq6ZV3kR"
      },
      "outputs": [],
      "source": [
        "#@title Plot settings\n",
        "\n",
        "plt.style.use('seaborn-ticks')\n",
        "plt.rc('xtick', labelsize=12)\n",
        "plt.rc('ytick', labelsize=12)\n",
        "plt.rc('legend', fontsize=10)\n",
        "plt.rc('axes', labelsize=15)\n",
        "plt.rc('axes', titlesize=16)\n",
        "plt.rc('figure', titlesize=16)\n",
        "plt.rc('text', usetex=False)\n",
        "plt.rcParams['pdf.fonttype'] = 42\n",
        "plt.rcParams['ps.fonttype'] = 42\n",
        "plt.rc('grid', linestyle=\"-\", color='#CCCCCC')"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 0,
      "metadata": {
        "colab": {
          "autoexec": {
            "startup": false,
            "wait_interval": 0
          }
        },
        "colab_type": "code",
        "id": "dSHv_jabWD5A"
      },
      "outputs": [],
      "source": [
        "#@title Plotting functions \n",
        "\n",
        "def my_tsplot(df,\n",
        "              time=\"x\",\n",
        "              value=\"y\",\n",
        "              condition=None,\n",
        "              title=\"\",\n",
        "              err_style=\"ci_band\",\n",
        "              xlims=None,\n",
        "              ylims=None,\n",
        "              ax=None,\n",
        "              legend=True,\n",
        "              color=None,\n",
        "              subplot=(1, 1, 1)):\n",
        "  \"\"\"Plotting method.\"\"\"\n",
        "  if ax is None:\n",
        "    _, ax = plt.subplots(subplot[0], subplot[1], figsize=(10, 6))\n",
        "  sns.tsplot(\n",
        "      df,\n",
        "      time=time,\n",
        "      value=value,\n",
        "      condition=condition,\n",
        "      err_style=err_style,\n",
        "      legend=legend,\n",
        "      estimator=np.nanmax,\n",
        "      ax=ax,\n",
        "      ci=100,\n",
        "      color=color)\n",
        "  ax.set_title(title)\n",
        "\n",
        "  if xlims is not None:\n",
        "    ax.set_xlim(xlims)\n",
        "  if ylims is not None:\n",
        "    ax.set_ylim(ylims)\n",
        "\n",
        "  ax.set_xlabel(\"steps\")\n",
        "  ax.set_ylabel(value)\n",
        "\n",
        "  if legend:\n",
        "    ax.legend(bbox_to_anchor=(1, 1), loc=\"upper left\")\n",
        "  format_axes(ax)\n",
        "  return ax\n",
        "\n",
        "def format_axes(ax):\n",
        "  \"\"\"Axes formatting.\"\"\"\n",
        "  ax.spines['top'].set_visible(False)\n",
        "  ax.spines['right'].set_visible(False)\n",
        "  ax.yaxis.set_ticks_position('left')\n",
        "  ax.xaxis.set_ticks_position('bottom')\n",
        "  ax.grid(True)\n",
        "  return ax\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 0,
      "metadata": {
        "colab": {
          "autoexec": {
            "startup": false,
            "wait_interval": 0
          }
        },
        "colab_type": "code",
        "id": "QLkoCB78Rscb"
      },
      "outputs": [],
      "source": [
        "#@title Execute Training scripts\n",
        "# Invoke the training script for RMC on Nth Farthest.\n",
        "\n",
        "# Load in training script\n",
        "\n",
        "# Train with default options.\n",
        "from sonnet.examples import rmc_nth_farthest\n",
        "\n",
        "steps, train_losses, test_accs = rmc_nth_farthest.build_and_train()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 0,
      "metadata": {
        "colab": {
          "autoexec": {
            "startup": false,
            "wait_interval": 0
          }
        },
        "colab_type": "code",
        "id": "_izB6AqMjttk"
      },
      "outputs": [],
      "source": [
        "#@title Plot Training Loss \u0026 Test Accuracy \n",
        "# Plot Test Accuracy and Training Loss against training steps.\n",
        "\n",
        "df_test_accuracy = pd.DataFrame(data={'x': steps, 'y': test_accs})\n",
        "df_train_loss = pd.DataFrame(data={'x': steps, 'y': train_losses})\n",
        "\n",
        "my_tsplot(df_test_accuracy, title=\"RMC Nth Farthest - Test Set Accuracy\")\n",
        "my_tsplot(df_train_loss, title=\"RMC Nth Farthest - Training Set Loss\")"
      ]
    }
  ],
  "metadata": {
    "colab": {
      "collapsed_sections": [],
      "default_view": {},
      "name": "Relational Memory Core Nth Farthest task",
      "provenance": [
        {
          "file_id": "1JB4nO80gE7mCfzRyZ0dzlQyT-3j4CkZP",
          "timestamp": 1527771383976
        },
        {
          "file_id": "1ZzjE4jgvpbOgU5Komjby0wDnVN6nsERA",
          "timestamp": 1523987033906
        },
        {
          "file_id": "1ySogp7DmX8r0Spru3e_yG2fThsVpxTlE",
          "timestamp": 1523633660599
        },
        {
          "file_id": "1HPXMYWSyUzyvGTXP780o7jlja84QZGi3",
          "timestamp": 1520546819048
        }
      ],
      "version": "0.3.2",
      "views": {}
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}
